package com.vancouver.knows.business;

import com.pangu.MobBeanManager;
import com.vancouver.knows.business.cache.CacheManager;
import com.vancouver.knows.business.service.impl.BHttpService;
import com.vancouver.knows.business.service.impl.BLocalService;
import com.vancouver.knows.business.service.impl.BProtocolFactory;
import com.vancouver.knows.business.task.BServiceWrapper;
import com.vancouver.knows.business.task.BTaskMarkPool;

/**
 * Bean管理器
 * <p/>
 * Created by cxlin
 */
public class BeanManager extends MobBeanManager {

	private BusinessModule module;

	public BeanManager(BusinessModule module) {
		super(module.getAMApplication());
		this.module = module;
	}

	@Override
	protected <T> T createBean(Class<T> clazz, String mark) {
		Object obj = null;
		if (clazz == BSharedPrefManager.class) {
			obj = new BSharedPrefManager(imContext);

		} else if (clazz == BServiceWrapper.class) {
			obj = new BServiceWrapper(module, getLocalService());

		} else if (clazz == BLocalService.class) {
			obj = new BLocalService(getHttpService(), module);

		} else if (clazz == BHttpService.class) {
			obj = new BHttpService(imContext, getProtocolFactory());

		} else if (clazz == BProtocolFactory.class) {
			obj = new BProtocolFactory();

		} else if (clazz == BTaskMarkPool.class) {
			obj = new BTaskMarkPool();

		} else if (clazz == CacheManager.class) {
			obj = new CacheManager();

		} else {
			obj = super.createBean(clazz, mark);
		}

		return (T) obj;
	}

	/**
	 * 获取配置文件
	 * 
	 * @return
	 */
	public BSharedPrefManager getSharedPrefManager() {
		return getBean(BSharedPrefManager.class);
	}

	/**
	 * 获取服务代理封装类
	 * 
	 * @return
	 */
	public BServiceWrapper getServiceWrapper() {
		return getBean(BServiceWrapper.class);
	}

	/**
	 * 本地服务类
	 * 
	 * @return
	 */
	private BLocalService getLocalService() {
		return getBean(BLocalService.class);
	}

	/**
	 * 远程资源的服务类
	 * 
	 * @return
	 */
	private BHttpService getHttpService() {
		return getBean(BHttpService.class);
	}

	/**
	 * 与服务的交互的所有协议数据
	 * 
	 * @return
	 */
	private BProtocolFactory getProtocolFactory() {
		return getBean(BProtocolFactory.class);
	}

	/**
	 * 任务池对象
	 * 
	 * @return
	 */
	public BTaskMarkPool getTaskMarkPool() {
		return getBean(BTaskMarkPool.class);
	}

	/**
	 * 获取缓存管理器
	 * 
	 * @return
	 */
	public CacheManager getCacheManager() {
		return getBean(CacheManager.class);
	}

}
